import inspect
from common.KoufuConf import KoufuConf

__author__ = 'yangxianming'



class KoufuShop20Api:
    __koufu_conf = KoufuConf()

    @staticmethod
    def __update_body(body, update_body="", del_body_key=""):
        if str(type(body)) == "<class 'list'>":
            body = update_body
            return body
        #更新或删除接口数据
        if update_body != "":
            body.update(update_body)
        if del_body_key != "":
            if "," in del_body_key:
                for one_key in del_body_key.split(","):
                    del body[one_key]
            else:
                del body[del_body_key]
        return body

    @staticmethod
    def __post_body_to_get_body(body):
        get_str = ""
        for (key, value) in body.items():
            get_str = get_str + "&" + key + "=" + str(value)
        get_str = get_str[1:]
        return get_str

    @staticmethod
    def __get_form_data(body, boundary):
        #将数据转换为 form_data 的body格式
        form_data = ""
        for (key, value) in body.items():
            form_data = form_data + "--" + boundary + "\r\n"
            form_data = form_data + "Content-Disposition: form-data; name=\"" + key + "\"\r\n"
            form_data += "Content-Type: text/plain; charset=UTF-8\r\n"
            form_data += "Content-Transfer-Encoding: 8bit\r\n\r\n"
            form_data = form_data + value + "\r\n"
        form_data = form_data + "--" + boundary + "--\r\n\r\n\r\n"
        # print("|" + form_data + "|")
        return form_data

    #update_body:{}  del_body_key:key值 使用,分割多个值


    #口福商城后管登陆
    def shop_login(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/sys/login1"
        # default_body = "username=puhuitongAdmin&password=ddyjpyc392925488b28ab12584ac8fcaa8a27a0f497b2c62940c8f4fbc8ef19ebc87c43e8ivzy1"
        default_body = "username=yangxianming&password=8rrrkuiq92925488b28ab12584ac8fcaa8a27a0f497b2c62940c8f4fbc8ef19ebc87c43e9v0d4m"
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #创建商品
    def get_goods_save(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/goods/save"
        default_body = {
            "primaryPicUrl": "https://efulidev.oss-cn-hangzhou.aliyuncs.com/koufu_shop_dev/20210906/10552942075380.jpg",
	        "listPicUrl": "",
	        "categoryId": "",
	        "isOnSale": "1",
	        "isNew": 1,
	        "isAppExclusive": 0,
	        "isLimited": 0,
	        "isHot": 0,
	        "categoryName": "一级分类",
	        "presell": "01",
	        "timing": "N",
	        "goodsType": "01",
	        "needCert": "N",
	        "safeguard1": [],
            "stoppingArea": "",
	        "delivery": False,
	        "goodsSn": "AT0001",
	        "name": "易福励走地鸡",
	        "limitType": "01",
	        "deadLineDate": "",
	        "goodsDesc": "",
	        "useExplain": "",
	        "goodsImgList": []
            }
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #删除商品
    def get_goods_delete(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/goods/delete"
        default_body = [""]
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #口福商场获取商品列表 2021-09-03
    def get_goods_list(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/goods/list"
        default_body = {
            "goodsType":"01",
            "_search":"false",
            "nd":"1630655122249",
            "limit":"10",
            "page":"1",
            "sidx":"",
            "order":"asc",
            "_":"1630655121782",
            }
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #创建商品sku
    def get_product_save(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/product/save"
        default_body = {
            "goodsSn": "ATSKU0001",
	        "specialId": 4,
	        "specialValue": "1000个",
            "goodsNumber": 1000,
            "skuNumber": 1000,
	        "goodsId": "10050368",
	        "purchasePrice": 1,
	        "retailPrice": 1
            }
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #口福查询商品sku 2021-09-06
    def get_product_list(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/product/list"
        default_body = {
            "goodsId":"10050368",
            "_search":"false",
            "nd":"1630655122249",
            "limit":"10",
            "page":"1",
            "sidx":"",
            "order":"asc",
            "_":"1630655121782",
            }
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #获得供应商列表
    def get_syssupplier_list(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/syssupplier/list"
        default_body = {
            "_search":"false",
            "nd":"1630655122249",
            "limit":"10",
            "page":"1",
            "sidx":"",
            "name":"",
            "_":"1630655121782",
            }
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #编辑定时任务
    def get_sys_schedule_update(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/sys/schedule/update"
        default_body = {"jobId":52,
                        "beanName":"supplierOrderExportAndSendMailJob",
                        "methodName":"run",
                        "params":"",
                        "cronExpression":"10 3 15 * * ? *",
                        "status":1,
                        "remark":"单独执行定时器，请修改参数后执行",
                        "createTime":1620710930000
        }
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #执行定时任务
    def get_sys_schedule_run(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/sys/schedule/run"
        default_body = ["52"]
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #添加到标签中
    def get_attributecategoryrel_save(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/attributecategoryrel/save"
        default_body = {
            "productId":681,
	        "categoryId":"1036030"
            }
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #口福商城后管 商品列表 get
    def get_catagory_all_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/catagory/all"
        default_body = {"parentId":0,"request_id":"123456"}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #口福商城后管 添加商品类星
    def get_category_save_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/category/save"
        default_body = {"isShow":1,"type":0,"level":"L2","bannerUrl":"","iconUrl":"","imgUrl":"","wapBannerUrl":"","name":"电火锅","frontDesc":"电火锅","parentId":1036047,"showIndex":"1"}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #口福商城后管 商品列表 get
    def get_goods_list_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/goods/list"
        default_body = {"_search":"false","nd":"1563940342134","limit":"100","page":"1","sidx":"","order":"asc","_":"1563940341629"}
        if get_path == 1:
            return path
        return self.__post_body_to_get_body(self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key))

    #商城2.0 券详情查询
    #接口文档 http://api.ebuy.io/#/home/project/inside/api/detail?groupID=90&apiID=657&projectName=%E7%94%B5%E5%AD%90%E5%88%B8%E5%95%86%E5%9F%8E&projectID=31
    def get_api_order_voucherDetail_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/order/voucherDetail"
        default_body = {"voucherId":0,"requestId":"123456"}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #商城2.0 订单详情查询
    #接口文档 http://api.ebuy.io/#/home/project/inside/api/detail?groupID=90&childGroupID=93&apiID=656&projectName=%E7%94%B5%E5%AD%90%E5%88%B8%E5%95%86%E5%9F%8E&projectID=31
    def get_api_order_orderDetail_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/order/orderDetail"
        default_body = {"orderSn":"", "requestId":"123456"}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #商城2.0 支付宝调起支付接口
    #接口文档 http://api.ebuy.io/#/home/project/inside/api/detail?groupID=90&childGroupID=93&apiID=651&projectName=%E7%94%B5%E5%AD%90%E5%88%B8%E5%95%86%E5%9F%8E&projectID=31
    def get_api_pay_ali_pay_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/pay/smartPay"
        default_body = {"orderSn":"", "requestId":"123456","payId":"00"}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #商城 支付方式获取
    #接口文档 http://api.ebuy.io/#/home/project/inside/api/detail?groupID=-1&apiID=679&projectName=%E7%94%B5%E5%AD%90%E5%88%B8%E5%95%86%E5%9F%8E&projectID=31
    def get_api_pay_payment_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/pay/payment"
        default_body = {"requestId":"123456"}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #商城 聚合支付
    #接口文档 http://api.ebuy.io/#/home/project/inside/api/detail?groupID=-1&apiID=681&projectName=%E7%94%B5%E5%AD%90%E5%88%B8%E5%95%86%E5%9F%8E&projectID=31
    def get_api_pay_smartPay_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/pay/smartPay"
        default_body = {"payId":"02","orderSn":"","requestId":"123456"}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #商城2.0 直接购买(无购物车)
    #接口文档 http://api.ebuy.io/#/home/project/inside/api/detail?groupID=90&childGroupID=93&apiID=650&projectName=%E7%94%B5%E5%AD%90%E5%88%B8%E5%95%86%E5%9F%8E&projectID=31
    def get_api_order_atOnceSubmit_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/order/atOnceSubmit"
        default_body = {"productId":"","number":1,"requestId":1577439350084,"exchange":""}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #商城2.0 口福授权接口
    #接口文档 http://api.ebuy.io/#/home/project/inside/api/detail?groupID=90&apiID=649&projectName=%E7%94%B5%E5%AD%90%E5%88%B8%E5%95%86%E5%9F%8E&projectID=31
    def get_api_auth_koufu_authorize_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/auth/koufu_authorize"
        default_body = {"name":"yangxianming","channelId":"koufu1234567890","thirdUserId":"040351271326585810","corpId":self.__koufu_conf.corpId}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #/api/exchange/goods/seckill
    #秒杀活动商品列表
    def get_api_exchange_goods_seckill_data(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/exchange/goods/seckill"
        default_body = {"requestId":1577439350084}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #/api/goods/product/list
    #秒杀活动商品列表
    def get_api_goods_product_list(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/goods/product/list"
        default_body = {"request_id":1577439350084,"corpId":"dingf686af244fa39dcf35c2f4657eb6378f"}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #/api/goods/es/good/keywords
    #商品全文搜索关键字接口    "corpId":"dingf686af244fa39dcf35c2f4657eb6378f"
    def get_api_goods_es_good_keywords(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/goods/es/good/keywords"
        default_body = {"request_id":1604312892119,"page":1,"size":200,}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)

    #/api/goods/es/product/list
    #商品全文搜索关键字接口    "corpId":"dingf686af244fa39dcf35c2f4657eb6378f"
    def get_api_goods_es_product_list(self, get_path=0, update_body="", del_body_key=""):
        path = "/koufu-shop/api/goods/es/product/list"
        default_body = {"request_id":1604312892119,"page":1,"size":200,"saleTotalNum":"","descPrice":"","ascPrice":""}
        if get_path == 1:
            return path
        return self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)


    #商城2.0 支付支付回调mock接口 （废弃，开发内部删除了mock接口）
    def get_api_pay_alipay_notify_mock_data(self, get_path=0, boundary=__koufu_conf.boundary, update_body="", del_body_key=""):
        path = "/koufu-shop/api/pay/alipay/notify/mock"
        default_body = {"out_trade_no":"", "gmt_payment":"2019-07-15 11:15:00"}
        if get_path == 1:
            return path
        body = self.__update_body(body=default_body, update_body=update_body, del_body_key=del_body_key)
        return self.__get_form_data(body, boundary)

    def get_funtion_name(self):
        api_list = []
        for one in dir(self):
            if "_data" in one and "get_" in one:
                api_list.append(one[4:-5].replace("_", "."))
        return api_list
